package com.eascs.fp.uc.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.fp.common.entity.MemberEntity;
import com.eascs.fp.common.entity.MemberOperateLogEntity;
import com.eascs.fp.common.security.MD5;
import com.eascs.fp.common.vo.ModelResult;
import com.eascs.fp.operateLog.server.OperateLogService;
import com.eascs.fp.uc.server.MemberService;
import com.eascs.fp.uc.untils.CusAccessObjectUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;


@RequestMapping("/ucCenter/rest")
@RestController
public class MemberCenterRestController extends BaseRestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private OperateLogService operateLogService;
	
	public static ModelResult<MemberEntity> isLogin(HttpServletRequest request) {
		ModelResult<MemberEntity> result = new ModelResult<>();
		try {
			HttpSession session = request.getSession();
			Object obj = session.getAttribute("member");
			if (obj != null) {
				result.withModel((MemberEntity) obj);
			}
		} catch (Exception e) {
			result.withError("LoginRestController.isLogin", "session获取登陆用户失败");
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping(value = "/login")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> login(HttpServletRequest request, HttpServletResponse response, String account, String password) {
		ModelResult<Boolean> result=new ModelResult<>();
		if (account.isEmpty() || password.isEmpty()) {
			result.withError("param is null", "登陆参数不能为空");
			return BaseRestResult.buildRestResult(result);
		}
		String pw=MD5.md5Encode(password.trim());
		MemberEntity entity=new MemberEntity(account,pw);
		ModelResult<MemberEntity> modelResult=memberService.queryMemberEntity(entity);
		if (modelResult.isSuccess()) {
			MemberEntity member = modelResult.getModel();
			if (member != null) {
				try {
					String ip=CusAccessObjectUtil.getIpAddress(request);
					MemberOperateLogEntity logEntity=new MemberOperateLogEntity();
					logEntity.setCreateTime(new Date());
					logEntity.setOperater(member.getAccount());
					logEntity.setLocation(ip);
					logEntity.setOperateDetail(request.getRequestURL().toString());
					operateLogService.insertMemberOperateLog(logEntity);
				} catch (Exception e) {
					logger.info("request.controller.error:/uc/login,exception:[{}]", e.getMessage());
					logger.info("request.controller.error:/uc/login,exception:{}", e.getMessage());
				}
				HttpSession session = request.getSession();
				session.removeAttribute("member");
				session.setAttribute("member", member);
				result.withModel(true);
				return BaseRestResult.buildRestResult(result);
			}
		}
		result.withError("param error", "账号或密码不正确");
		return BaseRestResult.buildRestResult(result);
	}
	
	

}
